<?php

class Model_Listing_Boat extends Model_Listing implements Model_Listing_Interface
{
    protected static  $_collection = 'listing';


    protected static $_requirements = array(
     //   'em'  => array('Required', 'Validator:EmailAddress'), //Email
    );

    protected $_category = array();

    public function __construct($data = array(), $config = array()) {
        static::$_transcript = array_merge(array(
            'ma' => 'make',
            'fe' => 'feature',
            'lg' => 'length',
            'ym' => 'yearmanufactured',
            'npo'=> 'nopreviousowners',
            'sb' => 'saleby',
            'bt' => 'boattype',
            'bc' => 'boatcategory',
            'et' => 'enginetype',
            'fc' => 'fuelcapacity',
            'ft' => 'fueltype',
            'ti' => 'trailerincluded',
            'hm' => 'hullmaterial',
            'my' => 'modelyear',
            'arl1'  => 'area_l1',
            'arl2'  => 'area_l2',
         ), static::$_transcript);
        parent::__construct($data, $config);
    }
	public static function getTranslationFormField()
    {
        $arr = array(
			'tl_en' =>'text',
			'cs_en' =>'textarea',
		);

		return $arr;
    }
    public function postAnAddForm($options = array()) {
        $options = array_merge($options, array('category' => $this->category));
        return new Listing_Form_PostAnAddBoatForm($options);
    }

    public function getAdDetailPartialName()
    {
        return 'customization/boat.phtml';
    }

    public function getAdPreviewPartialName()
    {
        return $this->getAdDetailPartialName();
    }


    public static function getSearchResultPartialName()
    {
        return 'boats';
    }
    public static function getRefineSearchForm()
    {
        return new Search_Form_BoatRefineForm();
    }

   public static function getSearchTextTerms()
    {
        return array(
            'description' => '1.0',
            'title' => '7.0',
            'make_name' => '9.0',
            'model_name' => '9.0',
            'area_l2_name' => '9.0',
            'street' => '7.0',
            'boattype_name' => '7.0',
            'enginetype_name' => '7.0',
            'feature_name' => '5.0',
            'boattype_name' => '5.0',
            'boatcategory_name' => '5.0',
            'fuelcapacity_name' => '5.0',
            'fueltype_name' => '5.0',
            'trailerincluded_name' => '5.0',
            'hullmaterial_name' => '5.0',
        );
    }


    /*
     * return a list of all the possible values that should be extracted from the keyword search and be specified as exact term
     *
     */
    public static function getExactSeachTerms()
    {
       return array(
                'boattype_name'              => self::getBoatType(),
                'fueltype_name'        => self::getFuelType(),
                'feature_name'          => self::getFeature(),
                'enginetype_name' => self::getEngineType(),
           );
    }

    public static function processRefineSearchForm(&$get, $page, $per_page, &$query, &$fields, &$additionalParameters)
    {
        //echo '<pre>';print_r($_GET);exit;

        //$q       = isset($_GET['keyword']) ? strtolower($_GET['keyword']) : false;
        $results = false;
        $fields  = array('description', 'title', 'area_l2_name', 'area_l1_name', 'street');
        $values  = array();
        $additionalParameters = array();
        //$get     = $_GET;

                $search_terms = self::getSearchTextTerms();

        $exact_search_terms = self::getExactSeachTerms();

        $q      = '';
        $qq     = !empty($_GET['keyword']) ? $_GET['keyword'] : '';
        $to_add = ' ';

        //add the username
        if (!empty($get['keyword'])) {
            //$q = '(username_lower:"'.$get['keyword'].'"^50.0) ';
        }

        if (!empty($exact_search_terms)) {
            foreach($exact_search_terms as $name => $val) {
                foreach($val as $k => $v) {
                    if (preg_match('#('.trim($v).')#i', $get['keyword'], $match)) {
                        //var_dump($match, $v, $get['keyword'], str_ireplace( $v, '', $get['keyword']));
                        $get['keyword'] = str_ireplace( $v, '', $get['keyword']);
                        $q .= $to_add.str_replace('_name', '', $name) .':'.$k.'^5.0 ';
                        $to_add = ' OR ';
                    }
                }
            }
            if (!empty($q) && !empty($tmp)) {
                $q = ' OR ('.$tmp.') ';
            }
        }

        $pattern = '#(?<=([0-9]))\s+bedroom[s]?#iu';
        if (preg_match($pattern, strtolower($get['keyword']), $match)) {
            $qq = str_ireplace($match[1].$match[0], '"'.$match[1].$match[0].'"', $qq);
            $get['keyword'] = str_ireplace($match[1].$match[0], '', $get['keyword']);
            $tmp = ' bedroom:'.trim($match[1]).'^9.0 ';
            if (!empty($q)) {
                $q .= ' OR ('.$tmp.') ';
            } else {
                $q = $tmp;
            }
        }

        $to_add = ' ';

        //remove quoted words and add them to q
        $bak = $get['keyword'];


        $arr = preg_match_all('#\"(.*?)\"#i', $get['keyword'], $match);
        foreach ($match[1] as $v) {
            $q .= ' AND "'.$v.'"';
        }
        $get['keyword'] = preg_replace('#"(.*?)"#i', '', $get['keyword']);
        $qf = '';
        foreach($search_terms as $v => $weight) {
            $qf .= $v.'^'.$weight.' ';
        }

        if (!empty($qf)) {
            $additionalParameters['qf'] = $qf;
        }
        if (!empty($qq)) {
            $additionalParameters['qq']   = $qq;
        }

        $get['keyword'] = trim($get['keyword']);
        if (!empty($get['keyword'])) {
            if (!empty($q)) {
                $q.=' OR ';
            }
            $q .='(';
            foreach ($fields as $f) {
                $list = explode(' ', trim($get['keyword']));
                foreach($list as $vl) {
                    if (!empty($vl)) {
                        $q .= $to_add.''.$f.':'.$vl;
                        $to_add = ' OR ';
                    }
                }
            }
            $q.=') ';
        }


        $q.= ' OR _query_:"{!dismax qf=$qf v=$qq}" ';

        if (empty($_GET['keyword'])) {
            $q = '*:*';
        }

        $query = $q;
        $get['sort'] = empty($get['sort']) ? '' : $get['sort'];

        switch ($get['sort']) {
            case 'highest':
                $get['sort'] = 'highest';
                $sort = 'price desc';
                break;
            case 'lowest':
                $get['sort'] = 'lowest';
                $sort = 'is_free asc, price asc';
                break;
            case 'rel':
                $get['sort'] = 'rel';
                $sort = 'score desc';
                break;
            case 'recent':
                $get['sort'] = 'recent';
                $sort = 'posted_at desc';
                break;
            case 'status':
                $get['sort'] = 'status';
                $sort = 'status desc';
                break;
            default:
                if (!empty($get['keyword_boat'])) {
                    $get['sort'] = 'rel';
                    $sort         = 'score desc';
                } else {
                    $get['sort'] = 'recent';
                    $sort         = 'posted_at desc';
                }
        }

        $additionalParameters['fl']   = '*,score';
        $additionalParameters['sort'] = $sort;

        //Parameters to refine search
        $fq         = '';
        $valuesplus = array();

        //Has Image
        if (!empty($get['key_search_tools']['has_image'])) {
            $valuesplus['has_image'] = true;
        }
        //Preferred Seller
        if (!empty($get['agency_owner_boat'])) {
            $valuesplus['saleby'] = $get['agency_owner_boat'];
        }
        elseif (!empty($get['seller']['saleby'])) {
            $valuesplus['saleby'] = $get['seller']['saleby'];
        }

        // Sale by
        if (!empty($get['wantto_boat'])) {
            $buy_rent = $get['wantto_boat'];
        }
        elseif (!empty($get['want_to']['buy_rent'])) {
            $buy_rent = $get['want_to']['buy_rent'];
        }

        // Sale by
        if (!empty($buy_rent)) {
            if ($buy_rent == 'Buy') {
                //Sale Category
                $sales = array();
                $sale = Model_Category::one(array('id' => 'c594'));
                $sales1 = $sale->_id;
                $sale = Model_Category::one(array('id' => 'c460'));
                $sales2 = $sale->_id;
                $category = ' +(category:'.$sales1.' OR category:'.$sales2.') ';
            }
            elseif ($buy_rent == 'Rent') {
                $rents = array();
                $rent = Model_Category::one(array('id' => 'c459'));
                $rents1 = $rent->_id;
                $category = ' +category:'.$rents1;
            }
            else {
                $category =' ';
            }
        }


        //Force only Cars
        $valuesplus['type_obj_s'] = 'Model_Listing_Boat';

        //Boat Type
        if (!empty($get['performances']['key_search_tool_boattype'])) {
            $valuesplus['boattype'] = $get['performances']['key_search_tool_boattype'];
        }
        if (!empty($get['boattype'])) {
            $valuesplus['boattype'] = $get['boattype'];
        }

        //Boat Category
        if (!empty($get['performances']['key_search_tool_boatcategory'])) {
            $valuesplus['boatcategory'] = $get['performances']['key_search_tool_boatcategory'];
        }
        if (!empty($get['boatcategory'])) {
            $valuesplus['boatcategory'] = $get['boatcategory'];
        }

        //Make
        if (!empty($get['performances']['key_search_tool_make'])) {
            $valuesplus['make'] = $get['performances']['key_search_tool_make'];
        }
        if (!empty($get['make'])) {
            $valuesplus['make'] = $get['make'];
        }

        //Feature
        if (!empty($get['performances']['key_search_tool_feature'])) {
            $valuesplus['feature'] = $get['performances']['key_search_tool_feature'];
        }

        //Length
        if (!empty($get['performances']['key_search_tool_length'])) {
            $valuesplus['length'] = $get['performances']['key_search_tool_length'];
        }

        //Price
        if (isset($get['performances-price']) && isset($get['performances-price-1'])) {
            $price  = $get['performances-price'];
            $price1 = $get['performances-price-1'];
        }
        elseif (isset($get['price_boat']) && isset($get['price_boat-1'])) {
            $price  = $get['price_boat'];
            $price1 = $get['price_boat-1'];
        }
        if (isset($price) && isset($price1)) {
            $test1 = false;$test2 = false;
            if (empty($price1)) {
                $price1 = '*';
                $test1 = true;
            }
            if (empty($price)) {
                $price = '0';
                $test2 = true;
            }
            if (!$test1 || !$test2) {
                $valuesplus['price'] = '['.intval(str_replace(",","",$price)).' TO '.intval(str_replace(",","",$price1)).']';
            }
        }

        //Year Manufactured
        if (!empty($get['performances']['key_search_tool_yearmanufactured'])) {
            $valuesplus['yearmanufactured'] = $get['performances']['key_search_tool_yearmanufactured'];
        }
        if (!empty($get['yearmanufactured_boat'])) {
            $valuesplus['yearmanufactured'] = $get['yearmanufactured_boat'];
        }

        //Model Year
        if (!empty($get['optional']['key_search_tool_modelyear'])) {
            $valuesplus['modelyear'] = $get['optional']['key_search_tool_modelyear'];
        }

        //No of previous Owners
        if (!empty($get['performances']['key_search_tool_previousowners'])) {
            $valuesplus['nopreviousowners'] = $get['performances']['key_search_tool_previousowners'];
        }

        //Engine Type
        if (!empty($get['optional']['key_search_tool_enginetype'])) {
            $valuesplus['enginetype'] = $get['optional']['key_search_tool_enginetype'];
        }

        //Hull Material
        if (!empty($get['optional']['key_search_tool_hullmaterial'])) {
            $valuesplus['hullmaterial'] = $get['optional']['key_search_tool_hullmaterial'];
        }

        //Trailer Included
        if (!empty($get['optional']['key_search_tool_trailerincluded'])) {
            $valuesplus['trailerincluded'] = $get['optional']['key_search_tool_trailerincluded'];
        }

        //Fuel Type
        if (!empty($get['optional']['key_search_tool_fueltype'])) {
            $valuesplus['fueltype'] = $get['optional']['key_search_tool_fueltype'];
        }

        //Fuel Capacity
        if (!empty($get['optional']['key_search_tool_fuelcapacity'])) {
            $valuesplus['fuelcapacity'] = $get['optional']['key_search_tool_fuelcapacity'];
        }

        // District
        if (!empty($get['key_search_tools']['area_l2'])) {
            $valuesplus['area_l2'] = $get['key_search_tools']['area_l2'];
        }
        elseif (!empty($get['performances']['area_l2'])) {
            $valuesplus['area_l2'] = $get['performances']['area_l2'];
        }


        foreach($valuesplus as $k => $val) {
          $valuesplus[$k] = '+'.$k.':'.$val.' ';
        }

        if (!empty($valuesplus)) {
            $fq .= implode(' ', $valuesplus);
        }

        if (!empty($category)) {
            $fq .= $category;
        }

        if (!empty($fq)) {
            $additionalParameters['fq'] = $fq;
        }
    }


    public static function getMake()
    {
        $view = Zend_Registry::get('Zend_View');
        $make = array();
        $attr      = Model_Listing_Attribute::all(array('type' => 'boats', 'path' => array('$exists' => 0)))->sort(array('order' => 1, 'name' => 1));

        foreach($attr as $k => $loc) {
            if ($loc->name == "All") {
                $make[(string) ''] = $loc->name;
            }
            else {
                $make[(string) $loc->name] = $loc->name;
            }
        }

        return $make;
    }

    public static function getBoats($term)
    {
        $view = Zend_Registry::get('Zend_View');
        $make = array();
        $content = array();
        $make_search = new MongoRegex('/^'.$term.'.*$/ui');
        $attr      = Model_Listing_Attribute::all(array('type' => 'boats','nm_en' => $make_search))->sort(array('order' => 1, 'name' => 1));

        $i = 0;
        foreach($attr as $k => $loc) {
            $content ['id'] = $loc->name;
            $content ['label'] = $loc->name;
            $content ['value'] = $loc->name;
            $make[$i] = $content;
            $i++;
        }

        return $make;
    }

    public static function getBoatType($id = null)
    {
        $res = array('' => 'All') + self::getBoatTypePostAd();
        if ($id !== null) {
            return $res[$id];
        }        
        return $res;
    }

    public static function getBoatTypePostAd()
    {
        $make = array();
        $attr      = Model_Listing_Attribute::all(array('type' => 'boattype', 'path' => array('$exists' => false)))->sort(array('nm_en' => 1));
        $make[''] = "Select";

        foreach($attr as $k => $loc) {
            $make[(string) $loc->_id] = $loc->name;
        }

        return $make;
    }

    public static function getBoatCategory($boattype, $id = null)
    {
        $res = array('' => 'All') + self::getBoatCategoryPostAd($boattype);
        if ($id !== null) {
            return $res[(string)$id];
        }

        return $res;
    }

    public static function getBoatCategoryPostAd($boattype)
    {
        $model = array();
        $attr = Model_Listing_Attribute::all(array('path' => new MongoId($boattype)))->sort(array('or' => 1, 'nm_en' => 1));
        $model[''] = "Select";

        foreach($attr as $k => $loc) {
            $model[(string) $loc->_id] = $loc->name;
        }
        //exit;
        return $model;
    }

    public static function getFuelCapacity($id = null)
    {
        $res = array('' => 'All') + self::getFuelCapacityPostAd();

        if ($id !== null) {
            return $res[$id];
        }
        return $res;
    }

    public static function getFuelCapacityPostAd()
    {
        return array(
            1 => '1-5 Gallons',
            2 => '6-10 Gallons',
            3 => '11-20 Gallons',
            4 => '21-30 Gallons',
            5 => '31-40 Gallons',
            6 => '41-50 Gallons',
            7 => '51-75 Gallons',
            8 => '75-100 Gallons',
            9 => '101-150 Gallons',
            10 => '151-200 Gallons',
            11 => '200 Gallons+'
        );
    }

    public static function getFuelType($id = null)
    {
        $res = array('' => 'All') + self::getFuelTypePostAd();

        if ($id !== null) {
            return $res[$id];
        }
        return $res;
    }

    public static function getFuelTypePostAd()
    {
        return array(
            '' => 'Select',
            1 => 'Diesel',
            2 => 'Electric',
            3 => 'Gas',
            4 => 'Other'
        );
    }

    public static function getLength($id = null)
    {
        $res = array('' => 'All') + self::getLengthPostAd();

        if ($id !== null) {
            return $res[$id];
        }
        return $res;
    }

    public static function getLengthPostAd()
    {
        return array(
            '' => 'Select',
            1 => 'Under 13 ft',
            2 => '13- 18 ft',
            3 => '18.1-26 ft',
            4 => '26.1-40 ft',
            5 => '40 ft +'
        );
    }



    public static function getYearManufactured($id = null)
    {
        $res = array('' => 'All') + self::getYearManufacturedPostAd();

        if ($id !== null) {
            return $res[$id];
        }
        return $res;
    }

  public static function getYearManufacturedPostAd()
    {
        return array(
            '' => 'Select',
            2010 => 2010,
            2009 => 2009,
            2008 => 2008,
            2007 => 2007,
            2006 => 2006,
            2005 => 2005,
            2004 => 2004,
            2003 => 2003,
            2002 => 2002,
            2001 => 2001,
            2000 => 2000,
            1999 => 1999,
            1998 => 1998,
            1997 => 1997,
            1997 => 1996,
            1997 => 1997,
            1996 => 1996,
            1995 => 1995,
            1994 => 1994,
            1993 => 1993,
            1992 => 1992,
            1991 => 1991,
            1990 => 1990,
            1989 => 1989,
            1988 => 1988,
            1987 => 1987,
            1986 => 1986,
            1985 => 1985,
            1984 => 1984,
            1983 => 1983,
            1982 => 1982,
            1981 => 1981,
            1980 => 1980,
            1979 => 1979,
            1978 => 1978,
            1977 => 1977,
            1976 => 1976,
            1975 => 1975,
            1974 => 1974,
            1973 => 1973,
            1972 => 1972,
            1971 => 1971,
            1970 => 1970,
        );
    }

    public static function getPreviousOwners($id = null)
    {

        $res = array(
            0 => '0',
            1 => '1',
            2 => '2',
            3 => '3',
            4 => '4+',
        );

        if ($id !== null) {
            return $res[$id];
        }

        return $res;
    }

    public static function getFeature($id = null)
    {
        $res = array('' => 'All') + self::getFeaturePostAd();

        if ($id !== null) {
            return $res[$id];
        }

        return $res;
    }

    public static function getFeaturePostAd()
    {
        return array(
            '' => 'Select',
            1 => 'Aft Cabin',
            2 => 'Airboat',
            3 => 'Bay',
            4 => 'Bowrider',
            5 => 'Catamaran (Power)',
            6 => 'Center Console',
            7 => 'Cruiser (Power)',
            8 => 'Cuddy Cabin',
            9 => 'Deck',
            10 => 'Downeast',
            11 => 'Dual Console',
            12 => 'Flats',
            13 => 'Flybridge',
            14 => 'Houseboat',
            15 => 'Jet',
            16 => 'Jon',
            17 => 'Pilothouse',
            18 => 'Pontoon',
            19 => 'Rigid Inflatable',
            20 => 'Runabout',
            21 => 'Sedan Bridge',
            22 => 'Ski, Wakeboard',
            23 => 'Sport Fisherman',
            24 => 'Sunbridge',
            25 => 'Trawler'
        );
    }

    public static function getEngineType($id = null)
    {
        $res = array('' => 'All') + self::getEngineTypePostAd();

        if ($id !== null) {
            return $res[$id];
        }

        return $res;
    }

    public static function getEngineTypePostAd()
    {
        return array(
            1 => 'Direct Drive',
            2 => 'Jet Drive',
            3 => 'engine-type-none',
            4 => 'Other',
            5 => 'engine-type-single',
            6 => 'Single Inboard/Outboard',
            7 => 'Single Outboard',
            8 => 'Triple Outboard',
            9 => 'Twin',
            10 => 'Twin Inboard/Outboard',
            11 => 'Twin Outboard',
            12 => 'Triple Inboard/Outboard',
            13 => 'V-Drive'
        );
    }

    public static function getHullMaterial($id = null)
    {
        $res = array('' => 'All') + self::getHullMaterialPostAd();

        if ($id !== null) {
            return $res[$id];
        }

        return $res;
    }

    public static function getHullMaterialPostAd()
    {
        return array(
            1 => 'Aluminum',
            2 => 'Carbon Fiber',
            3 => 'Composite',
            4 => 'Ferro-Cement',
            5 => 'Fiberglass/GRP',
            6 => 'Hypalon',
            7 => 'Inflatable',
            8 => 'Other',
            9 => 'Plastic',
            10 => 'PVC',
            11 => 'Steel',
            12 => 'Wood'
        );
    }

    public static function getModelYear($id = null)
    {
        $res = array('' => 'All') + self::getModelYearPostAd();

        if ($id !== null) {
            return $res[$id];
        }

        return $res;
    }

    public static function getModelYearPostAd()
    {
        $view = Zend_Registry::get('Zend_View');
        $array = array();
        $array[''] = 'Select';
        for ($i=2010;$i>=1900;$i--)
        {
            if ($i == 1900) {
                $array['-1'] = $i;
            }
            else {
                $array[$i] = $i;
            }

        }
        return $array;
    }

    public static function getTrailerIncluded($id = null)
    {
        $res = array('' => 'All') + self::getTrailerIncludedPostAd();

        if ($id !== null) {
            return $res[$id];
        }

        return $res;
    }

    public static function getTrailerIncludedPostAd()
    {
        $view = Zend_Registry::get('Zend_View');
        return array(
            '' => 'Select',
            1 => 'Yes',
            2 => 'No',
            3 => 'Not Specified'
        );
    }

    public function getSolrDocument($document = null)
    {
        if (!$document instanceof Apache_Solr_Document) {
            $document = new Apache_Solr_Document();
        }
        $view = Zend_Registry::get('Zend_View');
        //Commons
        $document->make          = (string)$this->ma;
        if(!empty($this->ma)) {
            $document->make_name             = (string)$this->make;
        }

        $document->model         = (string)$this->mo;
        if(!empty($this->mo)) {
            $document->model_name            = (string)$this->model;
            if (is_array($this->model->tags)) {
                foreach($this->model->tags as $tags) {
                    $document->addField('model_name', (string)$tags);
                }
            }
        }

        $document->yearmanufactured = (int)$this->ym;
        $document->nopreviousowners = (int)$this->npo;

        if (!empty($this->et)) {
            $document->enginetype       = (int)$this->et;
        }

        if (!empty($this->dit)) {
            $document->drivetype = (int)$this->dit;
            $document->addField('drivetype_name', self::getDriveType($this->dit));
            $document->addField('drivetype_name', $view->t(self::getDriveType($this->dit), Model_Translate::TC));
        }

        foreach ((array)$this->cf as $data) {
            $document->addField('feature', (string)$data);
        }

//        $document->length           = (string)$this->lg;

        //Boats
        if (!empty($this->bt)) {
            $document->boattype = (string)$this->bt;
            $type = Model_Listing_Attribute::one(array('_id' => $this->bt));
            $document->addField('boattype_name', $type->name);
            $document->addField('boattype_name', $view->t($type->name, Model_Translate::TC));
        }

        $document->boatcategory     = (string)$this->bc;


        if (!empty($this->hm)) {
            $document->hullmaterial             = (string)$this->hm;
            $document->addField('hullmaterial_name', self::getHullMaterial($this->hm));
            $document->addField('hullmaterial_name', $view->t(self::getHullMaterial($this->hm), Model_Translate::TC));
        }

        $document->modelyear        = (int)$this->my;

        if (!empty($this->fu)) {
            $document->fueltype             = (string)$this->fu;
            $document->addField('fueltype_name', self::getFuelType($this->fu));
            $document->addField('fueltype_name', $view->t(self::getFuelType($this->fu), Model_Translate::TC));
        }

        $document->fuelcapacity     = (int)$this->fc;

        if (!empty($this->ti) && $this->ti == 1) {
            $document->trailerincluded  = (int)$this->ti;
        }


        return parent::getSolrDocument($document);
    }

    public function processRestForm()
    {
        $post = $_POST;

        //Make
        $model = self::getBoats($_POST['model']);
       /* 
print_r($model);
print_r($_POST['model']);
print_r($model[$_POST['model']]);
exit;
        */   
        /*     
        if (empty($_POST['make']) || empty($model)) {
            throw new Ac_Exception_MissingInformation('Make is empty');
        }
        */
        if (!empty($_POST['make'])) {
            $this->model = $_POST['make'];
        }

        //Nb previous user
        $nbprevious = self::getPreviousOwners();
        if ((empty($_POST['nbprevious']) && $_POST['nbprevious'] != 0) || empty($nbprevious[$_POST['nbprevious']])) {
            throw new Ac_Exception_MissingInformation('Number of previous user is empty');
        }
        if (!empty($_POST['nbprevious']) || $_POST['nbprevious'] == 0) {
            $this->numberpreviousowner = intval($_POST['nbprevious']);
        }

        //Boattype
        $boattype = self::getBoatTypePostAd();
  /*      
print_r($boattype);
print_r($_POST['boattype']);
print_r($boattype[$_POST['boattype']]);
exit;
*/ 
        if (empty($_POST['boattype']) || empty($boattype[$_POST['boattype']])) {
            throw new Ac_Exception_MissingInformation('Boat type invalid');
        }
        if (!empty($_POST['boattype'])) {
            $this->boattype = $_POST['boattype'];
        }

        //Boatcategory
        $boatcategory = self::getBoatCategoryPostAd($_POST['boattype']);
/*
print_r($boatcategory);
print_r($_POST['boatcategory']);
print_r($boatcategory[$_POST['boatcategory']]);
exit;
  */      
        if (empty($_POST['boatcategory']) || empty($boatcategory[$_POST['boatcategory']])) {
            throw new Ac_Exception_MissingInformation('Boat category invalid');
        }
        if (!empty($_POST['boatcategory'])) {
            $this->boatcategory = $_POST['boatcategory'];
        }

        //features
        $features = Model_Listing_Boat::getFeaturePostAd();
        if (empty($_POST['feature']) || empty($features[$_POST['feature']])) {
            throw new Ac_Exception_MissingInformation('Features invalid');
        }
        if(!empty($feature)) {
            $this->feature = $_POST['feature'];
        }

        if (!empty($_POST['year'])) {
            $this->year = intval($_POST['year']);
        }
        $price          = !empty($_POST['price']) ? $_POST['price'] : '';
        if (!empty($price) && $_POST['price'] != '' && !is_numeric($_POST['price']) ) {
            //echo 'price empty';exit;
            throw new Ac_Exception_MissingInformation('Price is not valid');
        }

        if ($price == 0 ){
            $this->price = 0;
        } elseif(empty($price)) {

        } else {
            $this->price = (float)$price;
        }
        parent::processRestForm();
        return true;
    }

    public function exportRest($compressed = false)
    {
        $res = parent::exportRest($compressed);
        $view = Zend_Registry::get('Zend_View');


        $res['type'] = 'boat';

        if ($compressed) {
            return $res;
        }
        
        /*
        if(!empty($this->model)) {
            $res['model'] = (string) $this->model->name;
        }
        $res['nbprevious'] = (string)$this->numberpreviousowner;
        $res['year'] = (string)$this->yearmanufactured;
      //  var_dump(self::getBoatTypePostAd());exit;
        if (!empty($this->boattype)) {
            $res['boattype'] = (string)$this->boattype;
            $res['boattype_en'] = (string)self::getBoatType((string)$this->boattype);
            $res['boattype_tc'] = (string)$view->t(self::getBoatType((string)$this->boattype), Model_Translate::TC);
        }

        //var_dump(self::getBoatCategoryPostAd($this->boattype));exit;
        if (!empty($this->boatcategory)) {
            $res['boatcategory'] = (string)$this->boatcategory;
            $res['boatcategory_en'] = (string)self::getBoatCategory($this->boattype, (string)$this->boatcategory);
            $res['boatcategory_tc'] = (string)$view->t(self::getBoatCategory($this->boattype, (string)$this->boatcategory), Model_Translate::TC);
        }
         */

        //Commons
        if(!empty($this->ma)) {
            $res['make'] = (string)$this->ma;
            $res['make_name'] = (string)$this->make;
        }

        if(!empty($this->mo)) {
            $res['model'] = (string)$this->mo;
            $res['model_name'] = (string)$this->model;
            if (!empty($this->model) && !empty($this->model->tags) && is_array($this->model->tags)) {
                foreach($this->model->tags as $tags) {
                    $res['model_name'] = (string)$tags;
                }
            }
        }

        $res['year'] = (string)$this->ym;
        $res['noprevious'] = (string)$this->npo;

        if (!empty($this->et)) {
            $res['enginetype'] = (string)$this->et;
        }

        if (!empty($this->dit)) {
            $res['drivetype']    = (string)$this->dit;
            $res['drivetype_en'] = self::getDriveType($this->dit);
            $res['drivetype_tc'] = $view->t(self::getDriveType($this->dit), Model_Translate::TC);
        }

        foreach ((array)$this->cf as $data) {
            $res['feature'] = (string)$data;
        }

        //Boats
        if (!empty($this->bt)) {
            $res['boattype']  = (string)$this->bt;
            $type = Model_Listing_Attribute::one(array('_id' => $this->bt));
            if (!empty($type)) {
                $res['boattype_en'] = $type->name;
                $res['boattype_tc'] = $view->t($type->name, Model_Translate::TC);
            }
        }

        $res['boatcategory'] = (string)$this->bc;


        if (!empty($this->hm)) {
            $res['hullmaterial']    = (string)$this->hm;
            $res['hullmaterial_en'] = self::getHullMaterial($this->hm);
            $res['hullmaterial_tc'] = $view->t(self::getHullMaterial($this->hm), Model_Translate::TC);
        }

        $res['modelyear'] = (string)$this->my;

        if (!empty($this->fu)) {
            $res['fueltype'] = (string)$this->fu;
            $res['fueltype_en'] = self::getFuelType($this->fu);
            $res['fueltype_tc'] = $view->t(self::getFuelType($this->fu), Model_Translate::TC);
        }

        $res['fuelcapacity'] = (string)$this->fc;

        if (!empty($this->ti) && $this->ti == 1) {
            $res['trailerincluded']  = (string)$this->ti;
        }        
        return $res;
    }
}
